﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI
{
    //扩展字段
    public partial class BaseController : Controller
    {
        //获取扩展字段对称值
        public Dictionary<string, string> get_field_article_getfields(int channel_id, int article_id, string strWhere)
        {
            return new BLL.article_attribute_field().GetFields(channel_id, article_id, strWhere);
        }

        //获取扩展字段显示名称（多项单选）
        public string get_field_article_radio(object fieldname, object value)
        {
            string title = string.Empty;
            BLL.article_attribute_field bll = new BLL.article_attribute_field();
            Model.article_attribute_field model = bll.GetModel(new { name = fieldname.ToString() });
            if (model == null || string.IsNullOrEmpty(value.ToString()))
            {
                return string.Empty;
            }
            string[] valArr = model.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            for (int i = 0; i < valArr.Length; i++)
            {
                string[] valItemArr = valArr[i].Split('|');
                if (valItemArr.Length == 2)
                {
                    if (valItemArr[1] == value.ToString())
                    {
                        title = valItemArr[0];
                    }
                }
            }
            return title;
        }

        //获取扩展字段显示名称（多项多选）
        public List<string> get_field_article_checkbox(string name, string value)
        {
            List<string> title = new List<string>();
            BLL.article_attribute_field bll = new BLL.article_attribute_field();
            Model.article_attribute_field model = bll.GetModel(new { name = name });
            if (model == null || string.IsNullOrEmpty(value))
            {
                return title;
            }
            string[] valArr2 = model.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            string[] selValArr2 = value.Split(',');
            for (int i = 0; i < valArr2.Length; i++)
            {
                string[] valItemArr2 = valArr2[i].Split('|');
                if (valItemArr2.Length == 2)
                {
                    if (selValArr2.Contains(valItemArr2[1]))
                    {
                        title.Add(valItemArr2[0]);
                    }
                }
            }
            return title;
        }

        //根据字段列名获取扩展字段信息 如果有值传入
        public Dictionary<string, string> get_field_value(string name, string type, string value)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            Model.article_attribute_field model = new BLL.article_attribute_field().GetModel(new { name= name });
            if (model == null)
            {
                return dic;
            }

            string[] valArr = model.item_option.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            string[] selValArr2 = !string.IsNullOrEmpty(value) ? value.Split(',') : model.default_value.Split(',');
            for (int i = 0; i < valArr.Length; i++)
            {
                string[] valItemArr = valArr[i].Split('|');
                if (valItemArr.Length == 2)
                {
                    bool is_select = string.IsNullOrEmpty(value) ? false : selValArr2.Contains(valItemArr[1]);
                    dic.Add(valArr[i], is_select ? type == "select" ? "selected" : "checked" : "");
                }
            }
            return dic;
        }

        //获取类别的扩展字段 只显示name、title
        public Dictionary<string, string> get_field_value(int category_id)
        {
            return new BLL.article_attribute_field().GetFieldDictionary(category_id);
        }

        //扩区扩展字段标题
        public string get_field_title(int id)
        {
            return new BLL.article_attribute_field().GetCount("title", "id=" + id);
        }
    }
}
